package com.kuzufang.modules.main;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuzufang.R;
import com.kuzufang.base.BaseActivity;
import com.kuzufang.base.BaseFragment;
import com.kuzufang.modules.main.room.FindRoomFragment;
import com.kuzufang.modules.main.roommate.FindRoommateFragment;
import com.kuzufang.modules.publish.PublishActivity;
import com.kuzufang.modules.search.SearchActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity
        implements NavigationFragment.onCloseDrawerLayoutListener {
    private final static int SHOW_TAB_COUNT = 2;
    private List<BaseFragment> mShowFragments = new ArrayList<BaseFragment>() {{
        add(FindRoommateFragment.getInstance());
        add(FindRoomFragment.getInstance());
    }};

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FloatingActionButton mFloatActionButton;
    private DrawerLayout mLeftDrawer;
    private NavigationView mLeftNavigationView;
    private Toolbar mToolbar;
    private RelativeLayout rlSearchView;
    private ImageView mLeftMenu;


    private OnDrawerCloseListener mOnDrawerCloseListener;
    String[] titles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mFloatActionButton = (FloatingActionButton) findViewById(R.id.fab);
        mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.main_tab);
        mLeftDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mLeftNavigationView = (NavigationView) findViewById(R.id.nav_view);
        rlSearchView = (RelativeLayout) findViewById(R.id.search_view);
        mLeftMenu = (ImageView) findViewById(R.id.left_menu);

        mFloatActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, PublishActivity.class));
            }
        });

        initUIData();
        initTabLayout();
        initListener();
        if (Build.VERSION.SDK_INT > 21) {
            mLeftDrawer.setStatusBarBackgroundColor(Color.parseColor("#009688"));
        }

    }

    private void initListener() {

        rlSearchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
            }
        });

        mLeftMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLeftDrawer.openDrawer(GravityCompat.START);
            }
        });

        mLeftDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                drawerView.setClickable(true);
                mOnDrawerCloseListener = null;
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                if (mOnDrawerCloseListener != null) {
                    mOnDrawerCloseListener.onDrawerClosed();
                }
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        mLeftDrawer.setFocusableInTouchMode(true);
    }

    private void initUIData() {
        titles = new String[]{getString(R.string.find_roommate), getString(R.string.find_room)};
        MainFragmentAdapter findAdapter = new MainFragmentAdapter(getSupportFragmentManager(), titles);
        mViewPager.setAdapter(findAdapter);

        mViewPager.setOffscreenPageLimit(1);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mLeftDrawer.isDrawerOpen(GravityCompat.START)) {
            mLeftDrawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    private class MainFragmentAdapter extends FragmentStatePagerAdapter {
        String[] titles;

        public MainFragmentAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            this.titles = titles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mShowFragments.get(position);
        }

        @Override
        public int getCount() {
            return SHOW_TAB_COUNT;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onClose(OnDrawerCloseListener listener) {
        if (mLeftDrawer.isDrawerOpen(GravityCompat.START)) {
            mLeftDrawer.closeDrawer(GravityCompat.START);
        }
        mOnDrawerCloseListener = listener;
    }

    @Override
    public void onCloseOnly() {

    }

    public interface OnDrawerCloseListener {
        void onDrawerClosed();
    }


    private void initTabLayout() {
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        for (int i = 0; i < 2; i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(R.layout.tab_textview);
            TabViewHolder holder = new TabViewHolder(tab.getCustomView());
            if (i == 0) {
                holder.tvTabName.setSelected(true);
                holder.tvTabName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            }
            holder.tvTabName.setText(titles[i]);
        }

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                final TabViewHolder holder = new TabViewHolder(tab.getCustomView());
                holder.tvTabName.setSelected(true);
                ValueAnimator valueAnimator = ValueAnimator.ofInt(12, 16).setDuration(300);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int sp = (int) animation.getAnimatedValue();
                        holder.tvTabName.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
                    }
                });
                valueAnimator.start();
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                final TabViewHolder holder = new TabViewHolder(tab.getCustomView());
                holder.tvTabName.setSelected(false);
                ValueAnimator valueAnimator = ValueAnimator.ofInt(16, 12).setDuration(300);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int sp = (int) animation.getAnimatedValue();
                        holder.tvTabName.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
                    }
                });
                valueAnimator.start();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    class TabViewHolder {
        TextView tvTabName;

        public TabViewHolder(View tabView) {
            tvTabName = (TextView) tabView.findViewById(R.id.tv_tab_name);
        }
    }
}
